package Data;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class SumPresentOperator {

	private ArrayList<SumPresentData> sumPresentList = new ArrayList<SumPresentData>();
	String filename = "D:\\SumPresentRecord.txt";
	
	public SumPresentOperator(){
		try{
			BufferedReader bf = new BufferedReader(new FileReader(filename));
			SumPresentData spd = null;
			String data = "";
			String[] record = null;
			while((data = bf.readLine()) != null){
				if(data.trim().equals("")){
					continue;
				}
				record = splitText(data);
				spd = new SumPresentData(Double.parseDouble(record[0]),Integer.parseInt(record[1]),Double.parseDouble(record[2]),record[3],record[4]);
				sumPresentList.add(spd);
				
			}
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	
	public String[] splitText(String s){
		String[] data = s.split(",");
		return data;
	}
	
	public ArrayList<SumPresentData> getSumPresentData(){
		return sumPresentList;
	}
	public void removeSumPresent(double sumPrice,int giftID,double giftNum,String sumPresentStart,String sumPresentEnd){
		String s = null;
		String s1 = "\r\n";
		try{
			BufferedWriter writer = new BufferedWriter(new FileWriter(new File(filename)));
			for(SumPresentData spd : sumPresentList){
				if(spd.getSumPrice()==sumPrice && spd.getGiftID()==giftID && spd.getGiftNum()==giftNum 
						&& spd.getSumPresentStart().equals(sumPresentStart) && spd.getSumPresentEnd().equals(sumPresentEnd)){
					continue;
				}
				s = spd.getSumPrice() + "," + spd.getGiftID() + "," + spd.getGiftNum() + "," + spd.getSumPresentStart() + "," +spd.getSumPresentEnd();
				writer.write(s);
				writer.write(s1);
			}
						
			writer.close();
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void savaSumPresent(double sumPrice,int giftID,double giftNum,String sumPresentStart,String sumPresentEnd){
		String s = null;
		String s1 = "\r\n";
		try{
			BufferedWriter writer = new BufferedWriter(new FileWriter(new File(filename)));
			sumPresentList.add(new SumPresentData(sumPrice, giftID, giftNum, sumPresentStart, sumPresentEnd));
			for(SumPresentData spd : sumPresentList){
				s = spd.getSumPrice() + "," + spd.getGiftID() + "," + spd.getGiftNum() + "," + spd.getSumPresentStart() + "," +spd.getSumPresentEnd();
				writer.write(s);
				writer.write(s1);
			}
						
			writer.close();
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}

}
